package com.chuangjiangx.member.coupon.ddd.domain.model;

import com.chuangjiangx.dddbase.Entity;
import com.chuangjiangx.dddbase.domain.shared.Timestamp;
import com.chuangjiangx.member.coupon.ddd.domain.model.CardCoverEnum;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrConfigId;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrCouponId;
import com.chuangjiangx.member.basic.ddd.domain.model.CustomRecharge;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author LeeXyan
 * @Date 2018/1/19 18:06
 **/
@Getter
public class MbrConfig extends Entity<MbrConfigId> {
    /**
     * 商户id
     */
    private MerchantId merchantId;

    /**
     * 自定义储值金额开关
     */
    private CustomRecharge customRecharge;

    /**
     * 关注公众号赠送积分
     */
    private Integer subscribePnGiftScore;
    /**
     * 储值功能开关
     * 1 - 开启
     * 0 - 关闭
     */
    private Integer storedFunc;

    /**
     * 是否储值卡消费累计积分
     * 1-是  0-否
     */
    private Integer cardConsumerGrantScore;
    /**
     * 封面选择
     */
    private CardCoverEnum cardCoverChoice;
    /**
     * 背景色
     */
    private String cardColour;
    /**
     * 背景图
     */
    private String cardPicture;
    /**
     * 卡名称
     */
    private String cardName;
    /**
     * 联系电话
     */
    private String contactNumber;
    /**
     * 会员卡特权说明
     */
    private String cardPrivilegeExplain;
    /**
     * 会员卡使用须知
     */
    private String cardUseNotice;
    /**
     * 赠送积分
     */
    private BigDecimal giftScore;
    /**
     * 赠送卡券id
     */
    private MbrCouponId giftMbrCouponId;
    /**
     * 微信同步开关
     */
    private SwicthEnum wxSyncSwitch;

    /**
     * repository 转换方法
     * @param mbrConfigId
     * @param merchantId
     * @param customRecharge
     * @param subscribePnGiftScore
     * @param cardConsumerGrantScore
     * @param cardCoverChoice
     * @param cardColour
     * @param cardPicture
     * @param cardName
     * @param contactNumber
     * @param cardPrivilegeExplain
     * @param cardUseNotice
     * @param giftScore
     * @param giftMbrCouponId
     * @param wxSyncSwitch
     * @param createTime
     * @param updateTime
     */
    public MbrConfig(MbrConfigId mbrConfigId,MerchantId merchantId, CustomRecharge customRecharge, Integer subscribePnGiftScore, Integer cardConsumerGrantScore, CardCoverEnum cardCoverChoice, String cardColour, String cardPicture, String cardName, String contactNumber, String cardPrivilegeExplain, String cardUseNotice, BigDecimal giftScore, MbrCouponId giftMbrCouponId, SwicthEnum wxSyncSwitch, Date createTime, Date updateTime) {
        setId(mbrConfigId);
        this.merchantId = merchantId;
        this.customRecharge = customRecharge;
        this.subscribePnGiftScore = subscribePnGiftScore;
        this.cardConsumerGrantScore = cardConsumerGrantScore;
        this.cardCoverChoice = cardCoverChoice;
        this.cardColour = cardColour;
        this.cardPicture = cardPicture;
        this.cardName = cardName;
        this.contactNumber = contactNumber;
        this.cardPrivilegeExplain = cardPrivilegeExplain;
        this.cardUseNotice = cardUseNotice;
        this.giftScore = giftScore;
        this.giftMbrCouponId = giftMbrCouponId;
        this.wxSyncSwitch = wxSyncSwitch;
        this.setTimestamp(new Timestamp(createTime,updateTime));
    }

    public MbrConfig(MbrConfigId mbrConfigId, MerchantId merchantId, CustomRecharge customRecharge, Integer subscribePnGiftScore, Integer cardConsumerGrantScore, Date createTime, Date updateTime) {
        setId(mbrConfigId);
        this.merchantId = merchantId;
        this.customRecharge = customRecharge;
        this.subscribePnGiftScore = subscribePnGiftScore;
        this.cardConsumerGrantScore = cardConsumerGrantScore;
        this.setTimestamp(new Timestamp(createTime,updateTime));
    }

    /**
     * 会员信息初始化方法
     * @param merchantId
     * @param cardCoverChoice
     * @param cardColour
     * @param cardPicture
     * @param cardName
     * @param contactNumber
     * @param cardPrivilegeExplain
     * @param cardUseNotice
     * @param giftScore
     * @param giftMbrCouponId
     */
    public MbrConfig(MerchantId merchantId, CardCoverEnum cardCoverChoice, String cardColour
            , String cardPicture, String cardName, String contactNumber, String cardPrivilegeExplain
            , String cardUseNotice, BigDecimal giftScore, MbrCouponId giftMbrCouponId) {
        this.merchantId = merchantId;
        this.cardCoverChoice = cardCoverChoice;
        this.cardColour = cardColour;
        this.cardPicture = cardPicture;
        this.cardName = cardName;
        this.contactNumber = contactNumber;
        this.cardPrivilegeExplain = cardPrivilegeExplain;
        this.cardUseNotice = cardUseNotice;
        this.giftScore = giftScore;
        this.giftMbrCouponId = giftMbrCouponId;
        this.wxSyncSwitch = SwicthEnum.OFF;
        this.setTimestamp(new Timestamp(new Date(),null));
    }

    /**
     * 更新会员基础信息
     * @param cardCoverChoice
     * @param cardColour
     * @param cardPicture
     * @param cardName
     * @param contactNumber
     * @param cardPrivilegeExplain
     * @param cardUseNotice
     * @param giftScore
     * @param giftMbrCouponId
     */
    public void update(CardCoverEnum cardCoverChoice, String cardColour
            , String cardPicture, String cardName, String contactNumber, String cardPrivilegeExplain
            , String cardUseNotice, BigDecimal giftScore, MbrCouponId giftMbrCouponId){
        this.cardCoverChoice = cardCoverChoice;
        this.cardColour = cardColour;
        this.cardPicture = cardPicture;
        this.cardName = cardName;
        this.contactNumber = contactNumber;
        this.cardPrivilegeExplain = cardPrivilegeExplain;
        this.cardUseNotice = cardUseNotice;
        this.giftScore = giftScore;
        this.giftMbrCouponId = giftMbrCouponId;
        this.wxSyncSwitch = SwicthEnum.OFF;
    }

    /**
     * 设置同步微信开关
     */
    public void setWxSyncSwitch(SwicthEnum wxSyncSwitch) {
        this.wxSyncSwitch = wxSyncSwitch;
    }

    /**
     * 启用自定义储值
     */
    public void onCustomStored() {
        this.customRecharge = CustomRecharge.OPEND;
        this.update();
    }

    /**
     * 禁用自定义储值
     */
    public void offCustomStored() {
        this.customRecharge = CustomRecharge.CLOSE;
        this.update();
    }

    public void changeCustomStored(CustomRecharge custom){
        this.customRecharge = custom;
        this.update();
    }

    public void changeCardConsumerGrantScore(Integer value){
        if(1 != value  && 0 != value){
            throw new IllegalArgumentException("该值只支持0或1");
        }
        this.cardConsumerGrantScore = value;
        this.update();
    }

    public void changeSubscribePnGiftScore(Integer value){
        if(value < 0){
            throw new IllegalArgumentException("该值不能小于0");
        }
        this.subscribePnGiftScore = value;
        this.update();
    }

    /**
     * 控制储值功能
     */
    public void controlStoredFunc(Integer status) {
        this.storedFunc = (1 == status ? 1 : 0);
        this.update();
    }
}
